<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Model;

/**
 * 打卡详细信息
 */
class ClockLog extends Model
{

    // 表名,不含前缀
    protected $name = 'clock_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'createtime_text',
    ];

    public function getTypeList()
    {
        return [1=>'上班卡',2=>'下班卡'];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        return !$value ? '' : $this->getTypeList()[$value];
    }


    public function getImageAttr($value)
    {
        return empty($value) ? '' : cdnurl($value,true);
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    //打卡信息
    public static function getUserTypeLog($user_id,$clock_id,$type)
    {
        return self::where(['user_id'=>$user_id,'clock_id'=>$clock_id,'type'=>$type])->find();
    }

    //添加打卡记录
    public static function addClockLog($user_id,$clock_id,$type,$image,$address)
    {
        return self::create([
            'user_id'       =>$user_id,
            'clock_id'      =>$clock_id,
            'type'          =>$type,
            'image'         =>$image,
            'address'       =>$address,
        ]);
    }


}
